package fu.capstone.hanoicityguide.presentation.fragment.restaurant;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.UiLifecycleHelper;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.restaurant.RestaurantBO;
import fu.capstone.hanoicityguide.business.bo.restaurant.RestaurantBOImp;
import fu.capstone.hanoicityguide.business.model.Restaurant;
import fu.capstone.hanoicityguide.common.utility.InternetSetting;
import fu.capstone.hanoicityguide.common.utility.SharingFacebook;
import fu.capstone.hanoicityguide.presentation.adapter.ViewPagerAdapter;
import fu.capstone.hanoicityguide.presentation.fragment.home.HomeFragment;

;

@SuppressLint("ValidFragment")
public class DetailRestaurantFragment extends Fragment {

	private RestaurantBO restaurantBO;
	private int restaurantID;
	private ViewPager imgFragment;
	private ViewPagerAdapter vpagerAdapter;
	// private TextToSpeech tts;
	private TextView resDescription;
	private TextView resInformation;
	private UiLifecycleHelper uiHelper;
	private InternetSetting internetSetting;
	private Restaurant restaurant;

	public DetailRestaurantFragment() {

	}

	public DetailRestaurantFragment(int restaurantID) {
		this.restaurantID = restaurantID;
	}

	public int getRestaurantID() {
		return this.restaurantID;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		// if (tts != null) {
		// tts.stop();
		// tts.shutdown();
		// }
		super.onDestroy();
		if (uiHelper != null) {
			uiHelper.onDestroy();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View browseRestaurantFragment = inflater.inflate(
				R.layout.fragment_detail_restaurant, container, false);
		try {
			setHasOptionsMenu(true);
			internetSetting = new InternetSetting();
			// tts = new TextToSpeech(container.getContext(), this);
			// list Restaurant by filter and sorter


			restaurantBO = new RestaurantBOImp(container.getContext());

			// get data from database
			restaurant = restaurantBO.browseRestaurant(getRestaurantID());

			// fill data into components
			resDescription = (TextView) browseRestaurantFragment
					.findViewById(R.id.txtResDescription);
			resInformation = (TextView) browseRestaurantFragment
					.findViewById(R.id.txtResInformation);

			resDescription.setText(restaurant.getDescription());

			String information = "";
			if (!"".equals(restaurant.getRestaurantClass().trim())) {
				information = information.concat("Class : ");
				information = information.concat(restaurant.getRestaurantClass()
						.trim());
				information = information.concat("\n");
			}
			if (!"".equals(restaurant.getAddress().trim())) {
				information = information.concat("Address : ");
				information = information.concat(restaurant.getAddress().trim());
				information = information.concat("\n");
			}
			if (!"".equals(restaurant.getPhoneNumber().trim())) {
				information = information.concat("Phone number : ");
				information = information
						.concat(restaurant.getPhoneNumber().trim());
				information = information.concat("\n");
			}
			if (!"".equals(restaurant.getWebsite().trim())) {
				information = information.concat("Website : ");
				information = information.concat(restaurant.getWebsite().trim());
				information = information.concat("\n");
			}

			if (!"".equals(restaurant.getOpenTime().trim())
					&& !"".equals(restaurant.getOpenTime())) {
				information = information.concat("Officehour : ");
				information = information.concat(restaurant.getOpenTime());
				information = information.concat(" - ");
				information = information.concat(restaurant.getCloseTime());
				information = information.concat("\n");
			}
			resInformation.setText(information);

			restaurant.getArrImage();

			imgFragment = (ViewPager) browseRestaurantFragment
					.findViewById(R.id.restaurantPager);

			vpagerAdapter = new ViewPagerAdapter(getActivity(),
					restaurant.getArrImage());

			imgFragment.setAdapter(vpagerAdapter);
		} catch (Exception e) {
			Log.e("DetailRestaurantFragment", e.toString());
		}
		

		return browseRestaurantFragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.detailed_restaurant_page_menu, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Fragment fragment = new RestaurantOnlineMapFragment();
		internetSetting = new InternetSetting(getActivity());
		switch (item.getItemId()) {
		case R.id.action_map: {

			if (internetSetting.getConnectivityStatus(getActivity())) {
				fragment = new RestaurantOnlineMapFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("RestaurantID", restaurantID);
				fragment.setArguments(bundle);
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container, fragment)
							.commit();
					getActivity().setTitle("View on map");
				}
			}

			if (!internetSetting.getConnectivityStatus(getActivity())
					&& HomeFragment.isPurchased) {
				fragment = new RestaurantOfflineMapFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("RestaurantID", restaurantID);
				fragment.setArguments(bundle);
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container, fragment)
							.commit();
					getActivity().setTitle("View on map");
				}
			}

			if (!internetSetting.getConnectivityStatus(getActivity())
					&& !HomeFragment.isPurchased) {
				internetSetting.showSettingsAlert();
			}
		}
			break;
		case R.id.action_checkin: {
			if (internetSetting.getConnectivityStatus(getActivity())) {
				uiHelper = new UiLifecycleHelper(getActivity(), null);
				SharingFacebook.shareFacebook(uiHelper, getActivity());
			} else {
				internetSetting.showSettingsAlert();
			}
		}
			break;
		case R.id.action_contact: {
			if (restaurant != null
					&& (!restaurant.getPhoneNumber().trim().equals("") || !restaurant
							.getWebsite().trim().equals(""))) {

				AlertDialog.Builder alertDialog = new AlertDialog.Builder(
						getActivity());

				// Setting Dialog Title
				alertDialog.setTitle("Contact to " + restaurant.getName());

				// Setting Dialog Message
				alertDialog
						.setMessage("Do you want to contact with this restaurant ?");

				if (!restaurant.getPhoneNumber().trim().equals("")) {
					// On pressing Call button
					alertDialog.setPositiveButton("Call",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Intent intent = new Intent(
											Intent.ACTION_CALL);
									intent.setData(Uri.parse("tel:"
											+ restaurant.getPhoneNumber()));
									getActivity().startActivity(intent);
								}
							});
				}

				if (!restaurant.getWebsite().trim().equals("")) {
					alertDialog.setNeutralButton("Go to Website",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Intent intent = new Intent(
											Intent.ACTION_VIEW);
									String link = restaurant.getWebsite();
									if (!link.startsWith("http://")
											&& !link.startsWith("https://")) {
										link = "http://" + link;
									}
									intent.setData(Uri.parse(link));
									startActivity(intent);
								}
							});
				}

				// on pressing cancel button
				alertDialog.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						});

				// Showing Alert Message
				alertDialog.show();

			} else {
				Toast.makeText(getActivity(), "No contact information !",
						Toast.LENGTH_SHORT).show();
			}

			break;
		}
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getActivity().getActionBar().setTitle(
				restaurantBO.browseRestaurant(restaurantID).getName());
	}
}
